﻿# Class that creates PONO class that contains all the fields for SharePoint CSOM List object

class SPCoderListItem:
    "The class responsible for handling one list item"
    def __init__(self, item):
        self._ObjectItem = item
    
    def __setattr__(self,  name, value):
        self.__dict__[name] = value
    
class SPCoderList:
    "The class responsible for handling SharePoint CSOM list"
    def __init__(self, my_list):
        #get attributes from CSOM 
        self._ObjectItem = my_list
        my_context = my_list.Context
        my_context.Load(my_list.DefaultView)
        my_context.Load(my_list.DefaultView.ViewFields)
        my_context.ExecuteQuery()

        my_caml = CamlQuery()
        my_caml.ViewXml = my_list.DefaultView.ListViewXml
        my_items = my_list.GetItems(my_caml)

        my_context.Load(my_items)
        my_context.ExecuteQuery()
        
        self.Items = []
        for my_item in my_items:
            my_it = SPCoderListItem(my_item)
            for my_field in my_list.DefaultView.ViewFields:
                if (my_item.FieldValues.ContainsKey(my_field)):
                    my_it.__setattr__(my_field, my_item[my_field])
            self.Items.Add(my_it)